﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace MiniFox.Security
{
    /// <summary>
    /// 哈希加密服务
    /// </summary>
    abstract public class KeyedHashCryptoService : HashCryptoService
    {
        byte[] _key;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        protected KeyedHashCryptoService(string key) : this(Encoding.UTF8.GetBytes(key))
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        protected KeyedHashCryptoService(byte[] key) : base()
        {
            _key = key;
        }
        /// <summary>
        /// 
        /// </summary>
        public byte[] Key
        {
            get { return _key; }
        }

        /// <summary>
        /// 通过调用此方法，来设置加密算法
        /// </summary>
        /// <param name="algorithm"></param>
        protected override void SetHashAlgorithm(HashAlgorithm algorithm)
        {
            var alg = algorithm as KeyedHashAlgorithm;
            if (alg != null)
                alg.Key = this.Key;
        }
    }


}
